Revert "Fix for #3722"
authorEwan Higgs <ewan_higgs@yahoo.co.uk>
Wed, 29 Mar 2017 19:52:23 +0000 (21:52 +0200)
committerEwan Higgs <ewan_higgs@yahoo.co.uk>
Wed, 29 Mar 2017 19:52:23 +0000 (21:52 +0200)
This reverts commit 7e80c684cad3249923b05ecbe02c009b111d9b9f.

src/cargo/ops/cargo_new.rs
tests/init.rs

index 536b2bc50020034f9a1857da53364d861d966024..6838d690ab74fc6cebba04b81abdb63e569ddfd2 100644 (file)
@@ -11,7 +11,6 @@ use term::color::BLACK;
 use chrono::{Datelike,Local};
 use handlebars::{Handlebars, no_escape};
 use tempdir::TempDir;
-use toml;
 
 use core::Workspace;
 use sources::git::clone;
@@ -46,7 +45,6 @@ struct MkOptions<'a> {
     path: &'a Path,
     name: &'a str,
     bin: bool,
-    source_files: Vec<SourceFileInformation>,
 }
 
 impl Decodable for VersionControl {
@@ -130,40 +128,11 @@ fn get_input_template(config: &Config, opts: &MkOptions) -> CargoResult<Template
         // no template given, use either "lib" or "bin" templates depending on the
         // presence of the --bin flag.
         TemplateType::Builtin => {
-
-
-            let mut cargotoml_path_specifier = String::new();
-            // If the main function is in a weird location, let Cargo.toml know.
-            // Otherwise, if the library files are somewhere else, let Cargo.toml know.
-            for i in &opts.source_files {
-                if i.bin {
-                    if i.relative_path != "src/main.rs" {
-                        cargotoml_path_specifier.push_str(&format!(r#"
-[[bin]]
-name = "{}"
-path = {}
-"#, i.target_name, toml::Value::String(i.relative_path.clone())));
-                    }
-                } else {
-                    if i.relative_path != "src/lib.rs" {
-                        cargotoml_path_specifier.push_str(&format!(r#"
-[lib]
-name = "{}"
-path = {}
-"#, i.target_name, toml::Value::String(i.relative_path.clone())));
-                    }
-                }
-            }
-
-            let mut template_files = create_generic_template(&cargotoml_path_specifier);
-            let mut source_files = match (opts.bin, cargotoml_path_specifier.len()) {
-                (true, 0)  => create_bin_template(),
-                (true, _)  => vec![],
-                (false, 0) => create_lib_template(),
-                (false, _)  => vec![],
+            let template_files = if opts.bin {
+                create_bin_template()
+            } else {
+                create_lib_template()
             };
-            template_files.extend(source_files.drain(..));
-
             TemplateSet {
                 template_dir: None,
                 template_files: template_files
@@ -370,7 +339,6 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
         path: &path,
         name: name,
         bin: opts.bin,
-        source_files: vec![],
     };
 
     mk(config, &mkopts).chain_error(|| {
@@ -438,7 +406,6 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> {
         path: &path,
         name: name,
         bin: src_paths_types.iter().any(|x|x.bin),
-        source_files: src_paths_types,
     };
 
     mk(config, &mkopts).chain_error(|| {
@@ -692,22 +659,21 @@ fn walk_template_dir(dir: &Path, cb: &mut FnMut(DirEntry) -> CargoResult<()>) ->
 /// Create a generic template
 ///
 /// This consists of a Cargo.toml, and a src directory.
-fn create_generic_template(extra_cargo_info: &str) -> Vec<Box<TemplateFile>> {
-    let mut cargo_toml_contents = String::from(r#"[package]
+fn create_generic_template() -> Vec<Box<TemplateFile>> {
+    let template_file = Box::new(InMemoryTemplateFile::new(PathBuf::from("Cargo.toml"),
+    String::from(r#"[package]
 name = "{{name}}"
 version = "0.1.0"
 authors = [{{toml-escape author}}]
 
 [dependencies]
-"#);
-    cargo_toml_contents.push_str(extra_cargo_info);
-    let template_file = Box::new(InMemoryTemplateFile::new(PathBuf::from("Cargo.toml"),
-                            cargo_toml_contents));
+"#)));
     vec![template_file]
 }
 
 /// Create a new "lib" project
 fn create_lib_template() -> Vec<Box<TemplateFile>> {
+    let mut template_files = create_generic_template();
     let lib_file = Box::new(InMemoryTemplateFile::new(PathBuf::from("src/lib.rs"),
     String::from(r#"#[cfg(test)]
 mod tests {
@@ -716,17 +682,20 @@ mod tests {
     }
 }
 "#)));
-    vec![lib_file]
+    template_files.push(lib_file);
+    template_files
 }
 
 /// Create a new "bin" project
 fn create_bin_template() -> Vec<Box<TemplateFile>> {
+    let mut template_files = create_generic_template();
     let main_file = Box::new(InMemoryTemplateFile::new(PathBuf::from("src/main.rs"),
 String::from("fn main() {
     println!(\"Hello, world!\");
 }
 ")));
-    vec![main_file]
+    template_files.push(main_file);
+    template_files
 }
 
 #[cfg(test)]
index 9e0a34568296ccd93bf67f32865623840c42a138..47efa3b97c907c2724a30efd0e9df08851bf18ce 100644 (file)
@@ -63,7 +63,7 @@ fn both_lib_and_bin() {
                     "[ERROR] can't specify both lib and binary outputs"));
 }
 
-fn bin_already_exists(explicit: bool, rellocation: &str, needs_bin_section: bool) {
+fn bin_already_exists(explicit: bool, rellocation: &str) {
     let path = paths::root().join("foo");
     fs::create_dir_all(&path.join("src")).unwrap();
 
@@ -94,48 +94,36 @@ fn bin_already_exists(explicit: bool, rellocation: &str, needs_bin_section: bool
     let mut new_content = Vec::new();
     File::open(&sourcefile_path).unwrap().read_to_end(&mut new_content).unwrap();
     assert_eq!(Vec::from(content as &[u8]), new_content);
-
-    let mut cargo_content = String::new();
-    File::open(&paths::root().join("foo/Cargo.toml")).unwrap()
-        .read_to_string(&mut cargo_content).unwrap();
-    // Check that Cargo.toml has a bin section pointing to the correct location (if needed)
-    if needs_bin_section {
-        assert!(cargo_content.contains(r#"[[bin]]"#));
-        assert_that(&paths::root().join("foo/src/main.rs"), is_not(existing_file()));
-    } else {
-        assert!(!cargo_content.contains(r#"[[bin]]"#));
-        assert_that(&paths::root().join("foo/src/main.rs"), existing_file());
-    }
 }
 
 #[test]
 fn bin_already_exists_explicit() {
-    bin_already_exists(true, "src/main.rs", false)
+    bin_already_exists(true, "src/main.rs")
 }
 
 #[test]
 fn bin_already_exists_implicit() {
-    bin_already_exists(false, "src/main.rs", false)
+    bin_already_exists(false, "src/main.rs")
 }
 
 #[test]
 fn bin_already_exists_explicit_nosrc() {
-    bin_already_exists(true, "main.rs", true)
+    bin_already_exists(true, "main.rs")
 }
 
 #[test]
 fn bin_already_exists_implicit_nosrc() {
-    bin_already_exists(false, "main.rs", true)
+    bin_already_exists(false, "main.rs")
 }
 
 #[test]
 fn bin_already_exists_implicit_namenosrc() {
-    bin_already_exists(false, "foo.rs", true)
+    bin_already_exists(false, "foo.rs")
 }
 
 #[test]
 fn bin_already_exists_implicit_namesrc() {
-    bin_already_exists(false, "src/foo.rs", true)
+    bin_already_exists(false, "src/foo.rs")
 }
 
 #[test]
@@ -202,7 +190,7 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous
     assert_that(&paths::root().join("foo/Cargo.toml"), is_not(existing_file()));
 }
 
-fn lib_already_exists(rellocation: &str, needs_lib_section: bool) {
+fn lib_already_exists(rellocation: &str) {
     let path = paths::root().join("foo");
     fs::create_dir_all(&path.join("src")).unwrap();
 
@@ -225,39 +213,16 @@ fn lib_already_exists(rellocation: &str, needs_lib_section: bool) {
     let mut new_content = Vec::new();
     File::open(&sourcefile_path).unwrap().read_to_end(&mut new_content).unwrap();
     assert_eq!(Vec::from(content as &[u8]), new_content);
-
-    let mut cargo_content = String::new();
-    File::open(&paths::root().join("foo/Cargo.toml")).unwrap()
-        .read_to_string(&mut cargo_content).unwrap();
-    // Check that Cargo.toml has a lib section pointing to the correct location (if needed)
-    if needs_lib_section {
-        assert!(cargo_content.contains(r#"[lib]"#));
-        assert_that(&paths::root().join("foo/src/lib.rs"), is_not(existing_file()));
-    } else {
-        assert!(!cargo_content.contains(r#"[lib]"#));
-        assert_that(&paths::root().join("foo/src/lib.rs"), existing_file());
-    }
-
 }
 
 #[test]
 fn lib_already_exists_src() {
-    lib_already_exists("src/lib.rs", false)
+    lib_already_exists("src/lib.rs")
 }
 
 #[test]
 fn lib_already_exists_nosrc() {
-    lib_already_exists("lib.rs", true)
-}
-
-#[test]
-fn no_lib_already_exists_src_add_lib_section() {
-    lib_already_exists("src/foo.rs", true)
-}
-
-#[test]
-fn no_lib_already_exists_nosrc_add_lib_section() {
-    lib_already_exists("foo.rs", true)
+    lib_already_exists("lib.rs")
 }
 
 #[test]